﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;
using Newtonsoft.Json;
using ERP_SaaS_WinForm.采购管理.Server;
using ERP_SaaS_WinForm.Properties;
using static System.Windows.Forms.AxHost;
using ERP_SaaS_WinForm.生产计划_派单_车间.ProductPlan;

namespace ERP_SaaS_WinForm.采购管理
{
    public partial class Procurement : Form
    {
        private HttpClient httpClient;
        private int pageIndex = 1;
        private int pageSize = 15;
        private int totalCount = 0;
        private int totalPage = 0;
        private void UpdatePagingButtons()
        {
            Shou.Enabled = pageIndex > 1;
            Shang.Enabled = pageIndex > 1;
            Xia.Enabled = pageIndex < totalPage;
            Wye.Enabled = pageIndex < totalPage;
        }

        public Procurement()
        {
            InitializeComponent();
            InitializeHttpClient();
        }

        /// <summary>
        /// 初始化HttpClient
        /// </summary>
        private void InitializeHttpClient()
        {
            httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri("http://localhost:5117/");
        }

        /// <summary>
        /// 窗体加载完成事件
        /// </summary>
        private void Procurement_Load(object sender, EventArgs e)
        {
            // 设置标签页标题
            tabPage1.Text = "质检添加";
            tabPage2.Text = "质检显示";
        }
        /// <summary>
        /// 重置的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Chong_Click(object sender, EventArgs e)
        {
           
        }
        /// <summary>
        /// 查询的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private    async  void ChaXun_Click(object sender, EventArgs e)
        {
            string baseUrl = "http://localhost:5117/api/Supplier/GetQulityInfo";
            //编号
            string code = txtCode.Text.Trim();
            //质检物料
            string zhijian = ZHIJIA.SelectedValue?.ToString();
            //质检人员
            string ryuan = Ryuan.SelectedValue?.ToString();
            var queryParams = new List<string>();
            if (!string.IsNullOrEmpty(code)) queryParams.Add($"planCode={code}");
            if (!string.IsNullOrEmpty(zhijian)) queryParams.Add($"planName={zhijian}");
            if (!string.IsNullOrEmpty(ryuan)) queryParams.Add($"resource={ryuan}");
            queryParams.Add($"pageIndex={pageIndex}");
            queryParams.Add($"pageSize={pageSize}");
            string fullUrl = $"{baseUrl}?{string.Join("&", queryParams)}";
            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = await client.GetAsync(fullUrl);
                response.EnsureSuccessStatusCode();
                string jsonResponse = await response.Content.ReadAsStringAsync();
                ApiResponse apiResponse = JsonConvert.DeserializeObject<ApiResponse>(jsonResponse);
                if (apiResponse.code == 200)
                {

                    PlanData.DataSource = apiResponse.data;

                    totalCount = apiResponse.totalCount;
                    totalPage = apiResponse.totalPage;
                    


                    UpdatePagingButtons();

                }
                else
                {
                    MessageBox.Show($"查询失败{apiResponse.msg}");
                }
            }


        }
        /// <summary>
        /// 首页的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Shou_Click(object sender, EventArgs e)
        {
            if (pageIndex != 1)
            {
                pageIndex = 1;
                ChaXun_Click(sender, e);
            }
        }
        /// <summary>
        /// 上一页的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Shang_Click(object sender, EventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex--;
                ChaXun_Click(sender, e);
            }
        }
        /// <summary>
        /// 下一页的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Xia_Click(object sender, EventArgs e)
        {
            if (pageIndex < totalPage)
            {
                pageIndex++;
                ChaXun_Click(sender, e);
            }
        }
        /// <summary>
        /// 尾页的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Wye_Click(object sender, EventArgs e)
        {
            if (pageIndex != totalPage)
            {
                pageIndex = totalPage;
                ChaXun_Click(sender, e);
            }
        }
        /// <summary>
        /// 跳转到指定页的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Tiao_Click(object sender, EventArgs e)
        {
            //if (int.TryParse(txtPageNumber.Text, out int pageNum) && pageNum >= 1 && pageNum <= totalPage)
            //{
            //    pageIndex = pageNum;
            //    ChaXun_Click(sender, e);
            //}
            //else
            //{
            //    MessageBox.Show("请输入有效的页码。");
            //}
        }
    }

    /// <summary>
    /// 质检信息API响应结果
    /// </summary>
    public class ApiResult<T>
    {
        public int totalPage { get; set; }
        public int totalCount { get; set; }
        public object items { get; set; }
        public string msg { get; set; }
        public int code { get; set; }
        public List<T> data { get; set; }
    }
}
